<template>
  <div class="app-container">
    <div class="customContainer">
      <!-- 搜索头 -->
      <el-form :inline="true" :model="formInline" class="demo-form-inline" size="small">
        <el-form-item>
          <span style="color: #888;">状态：</span>
          <el-select v-model="formInline.is_use" style="width:90px" clearable>
            <el-option v-for="item in type_list" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-date-picker style="width:130px" :clearable="false" v-model="pickerStart" value-format="yyyy-MM" type="month" format="yyyy-MM"
            :picker-options="pickerOptions" placeholder="开始月份"></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-date-picker style="width:130px" :clearable="false" v-model="pickerEnd" value-format="yyyy-MM" type="month" format="yyyy-MM"
            :picker-options="pickerOptions" placeholder="结束月份"></el-date-picker>
        </el-form-item>
        <el-form-item>
          <span style="color: #888;">对接人：</span>
          <el-select v-model="formInline.butt_man_id" placeholder="请选择对接人" style="width:130px" clearable>
            <el-option v-for="item in docking_list" :key="item.id" :label="item.nick" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select v-model="searchType" style="width:130px" @change="searchChange">
            <el-option v-for="item in search_list" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
          <el-autocomplete clearable v-model="searchVal" style="width:200px" :fetch-suggestions="querySearch" :placeholder="searchPld" :trigger-on-focus="false"
            valueKey="name" size="small" @keyup.enter.native="searchClick"></el-autocomplete>
        </el-form-item>
        <el-form-item>
          <dj-button size="small" @search="searchClick" />
        </el-form-item>
        <el-form-item>
          <el-tooltip class="item" effect="dark" placement="bottom-end">
            <div slot="content" style="line-height:20px;width:400px">
              采购金额、欠货金额、退货金额全部为0时，查询结果不展示
            </div>
            <i class="el-icon-question"></i>
          </el-tooltip>
        </el-form-item>
      </el-form>
      <!-- 表格 -->
      <el-table highlight-current-row :data="list" v-loading.body="listLoading" border size="small">
        <el-table-column align="center" label="月份" prop="time" width="70"></el-table-column>
        <el-table-column align="center" label="供应商名称" prop="name"></el-table-column>
        <el-table-column align="center" label="供应商编号" prop="serial_number" width="90"></el-table-column>
        <el-table-column align="center" label="总部对接人" prop="butt_man" width="90"></el-table-column>
        <el-table-column align="center" label="采购金额" prop="all_money"></el-table-column>
        <el-table-column align="center" label="欠货金额" prop="lost_money"></el-table-column>
        <el-table-column align="center" label="采购欠货金额占比" prop="laRatio"></el-table-column>
        <el-table-column align="center" label="退货金额" prop="return_money"></el-table-column>
        <el-table-column align="center" label="退货采购金额占比" prop="raRatio"></el-table-column>
        <el-table-column align="center" label="付款类型">
          <template slot-scope="scope">
            <span>{{scope.row.pay_type|payTypeName}}</span>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 自定义底部 -->
    <div class="bottom_box">
      <el-pagination class="pull-left" background @current-change="handleCurrentChange" :current-page.sync="currentPage" layout="prev, pager, next, jumper"
        :page-count="totalDataNumber"></el-pagination>
    </div>
  </div>
</template>
<script>
import { getPage, getList } from '@/api/report/supInfoCollect';
import { getDocking, getSup } from '@/api/common/commonApi';
import { pickerOptionsMonth } from '@/utils';
export default {
  data() {
    return {
      list: null,
      totalList: [
        {
          name: '合计',
          all_money: '',
          adjust_money: '',
          use_money: '',
          amount_paid_money: '',
          kite_money: '',
          notpay_money: '',
          arrive_money: '',
          owe_money: ''
        }
      ],
      listLoading: false,
      type_list: [
        { id: '1', name: '在用' },
        { id: '2', name: '停用' }
      ],
      search_list: [
        { id: '1', name: '按供应商名称' },
        { id: '2', name: '按供应商编号' }
      ],
      docking_list: [],
      supplier_list: [], //供应商名称列表
      searchPld: '请输入供应商名称',
      searchType: '1',
      searchVal: '',
      formInline: {
        start_time: '',
        end_time: '',
        is_use: '1',
        butt_man_id: '',
        serial_number: '',
        supplier_name: ''
      },
      totalDataNumber: 1,
      currentPage: 1,
      pickerStart: new Date().toISOString().slice(0, 7),
      pickerEnd: new Date().toISOString().slice(0, 7),
      pickerOptions: pickerOptionsMonth()
    };
  },
  beforeRouteEnter(to, from, next) {
    next(vm => {
      if (!vm.list) {
        vm.getUserList();
      }
    });
  },
  methods: {
    searchChange(val) {
      this.searchVal = '';
      this.searchPld = val == 1 ? '请输入供应商名称' : '请输入供应商编号';
    },
    getUserList() {
      // this.listLoading = true;
      getDocking({}).then(res => {
        this.docking_list = res;
        this.searchClick();
        this.getSupName();
      });
    },
    //获取供应商名称数组
    getSupName() {
      getSup({}).then(res => {
        this.supplier_list = res;
      });
    },
    querySearch(queryString, cb) {
      var supplier_list = this.supplier_list;
      var results = queryString ? supplier_list.filter(this.createFilter(queryString)) : supplier_list;
      // 调用 callback 返回建议列表的数据
      cb(results);
    },
    createFilter(queryString) {
      return restaurant => {
        if (this.searchType == 1) {
          return restaurant.name.indexOf(queryString) >= 0;
        } else {
          return false;
        }
      };
    },
    // 搜索
    searchClick() {
      if (new Date(this.pickerStart).getTime() > new Date(this.pickerEnd).getTime()) {
        this.$message.error('开始月份不可大于结束月份');
        return;
      }
      if (new Date(this.pickerStart).getTime() + 8.64e7 * 30 * 12 < new Date(this.pickerEnd).getTime()) {
        this.$message.error('最长可选12个月');
        return;
      }
      this.formInline.start_time = this.pickerStart;
      this.formInline.end_time = this.pickerEnd;
      this.formInline.supplier_name = this.searchType === '1' ? this.searchVal : '';
      this.formInline.serial_number = this.searchType === '2' ? this.searchVal : '';
      this.fetchData();
    },
    getData(data) {
      data.forEach(val => {
        val.laRatio = val.all_money / 1 === 0 ? '-' : (Math.abs(val.lost_money / val.all_money) * 100).toFixed(2) + '%';
        val.raRatio = val.all_money / 1 === 0 ? '-' : (Math.abs(val.return_money / val.all_money) * 100).toFixed(2) + '%';
        val.all_money = (val.all_money / 1).toFixed(2);
        val.lost_money = (val.lost_money / 1).toFixed(2);
        val.return_money = (val.return_money / 1).toFixed(2);
      });
      this.list = data;
      this.listLoading = false;
    },
    // 列表获取
    fetchData() {
      this.listLoading = true;
      this.currentPage = 1;
      this.list = [];
      getPage(this.formInline)
        .then(response => {
          if (response.code == 1) {
            this.totalDataNumber = response.data.page;
            if (this.totalDataNumber > 0) {
              getList(this.formInline).then(res => {
                if (res.code == 1) {
                  this.getData(res.data);
                }
              });
            } else {
              this.listLoading = false;
            }
          }
        })
        .catch(res => {
          this.listLoading = false;
        });
    },
    handleCurrentChange(val) {
      this.listLoading = true;
      this.list = [];
      getList(this.formInline, val).then(res => {
        this.getData(res.data);
      });
    }
  }
};
</script>